package com.toune.movie.mvp.model;

import com.toune.movie.base.Api;
import com.toune.movie.base.AppConstant;
import com.toune.movie.base.myUtil.ThreadPoolManager;
import com.toune.movie.mvp.contract.MovieContract;
import com.toune.movie.mvp.presenter.MoviePresenter;
import com.toune.movie.vo.MovieContentVo;
import com.toune.movie.vo.VarietyVo;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/5/9.
 */

public class MovieModel implements MovieContract.Model {
    MoviePresenter presenter;

    public MovieModel(MoviePresenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void getDatas() {
        String url = "http://www.52xsba.com/";
        paseHtml(url);
    }

    private void paseHtml(final String url) {
        ThreadPoolManager.getInstatnce().createShortThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                try {
                    Document doc = Jsoup.connect(url).get();
                    Elements elementsByClass1 = doc.getElementsByClass("b-listtab-main");
                    List<MovieContentVo> movieContentVos = new ArrayList<>();
                    for (int i = 0; i < elementsByClass1.size(); i++) {
                        MovieContentVo movieContentVo = new MovieContentVo();
                        List<VarietyVo> varietyVos = new ArrayList<>();
                        Element element = elementsByClass1.get(i);
                        Elements lis = element.getElementsByTag("li");
                        if (lis == null || lis.size() <= 2) {
                            continue;
                        }
                        for (int j = 0; j < lis.size(); j++) {
                            VarietyVo varietyVo = new VarietyVo();
                            Element element1 = lis.get(j);
                            Elements as = element1.getElementsByTag("a");
                            String title = as.get(0).attr("title");
                            varietyVo.setTitle(title);
                            String detailUrl = Api.BaseUrl52 + as.get(0).attr("href");
                            varietyVo.setDetailUrl(detailUrl);
                            Elements img = element1.getElementsByTag("img");
                            String imgUrl = img.get(0).attr("src");
                            varietyVo.setImagSrc(imgUrl);
                            varietyVos.add(varietyVo);
                        }
                        movieContentVo.setDataBeans(varietyVos);
                        movieContentVos.add(movieContentVo);
                    }
                    Elements elementsByClass = doc.getElementsByClass("single-strong");
                    int key = 0;
                    for (int i = (elementsByClass.size() - movieContentVos.size()); i < elementsByClass.size(); i++) {
                        Element element = elementsByClass.get(i);
                        MovieContentVo movieContentVo = movieContentVos.get(key);
                        String url = element.text();
                        movieContentVo.setTitle(url);
                        movieContentVo.setTag(AppConstant.MovieTag[key]);
                        movieContentVo.setUrl(AppConstant.MovieUrls[key]);
                        key++;
                    }
                    presenter.setDatas(movieContentVos);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
